package com.ttg.web.dao.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ttg.common.config.DynamicTableNameHelper;
import com.ttg.common.dto.TagProducer;
import com.ttg.common.enums.BankPublicTradeEnum;
import com.ttg.model.mapper.BankPublicTradePrimitiveMapper;
import com.ttg.model.pojo.BankPrivatePrimitive;
import com.ttg.model.pojo.BankPublicTradePrimitive;
import com.ttg.web.dao.BankPublicTradePrimitiveDao;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Repository
public class BankPublicTradePrimitiveDaoImpl extends ServiceImpl<BankPublicTradePrimitiveMapper, BankPublicTradePrimitive> implements BankPublicTradePrimitiveDao {
    @Override
    public BankPublicTradePrimitive getByMctNoAndDateLast(String mctNo) {
        LambdaQueryWrapper<BankPublicTradePrimitive> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BankPublicTradePrimitive::getMctNo,mctNo);
        queryWrapper.orderByDesc(BankPublicTradePrimitive::getUpdateTime);
        queryWrapper.last("LIMIT 1");
        return getOne(queryWrapper);
    }

    @Override
    public BankPublicTradePrimitive getByMctNoAndTradeEnum(String mctNo, BankPublicTradeEnum bankPublicTradeEnum) {
        LambdaQueryWrapper<BankPublicTradePrimitive> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BankPublicTradePrimitive::getMctNo,mctNo);
        queryWrapper.eq(BankPublicTradePrimitive::getTradeType,bankPublicTradeEnum.getType());
        return getOne(queryWrapper);
    }

    @Override
    public void truncate() {
        baseMapper.truncate();
    }

    @Override
    public List<String> getMctNosByTags(List<TagProducer> tags, String key) {
        LambdaQueryWrapper<BankPublicTradePrimitive> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(BankPublicTradePrimitive::getMctNo);
        wrapper.orderByDesc(BankPublicTradePrimitive::getUpdateTime);
        for (TagProducer tag: tags) {
            wrapper.apply(key+tag.getCompareStr()+"{0}",tag.getValue());
        }
        List<BankPublicTradePrimitive> list = list(wrapper);
        if (Objects.isNull(list) || CollectionUtils.isEmpty(list)){
            return new ArrayList<>();
        }
        return list.stream().map(BankPublicTradePrimitive::getMctNo).collect(Collectors.toList());
    }


    @Override
    @Transactional(
            rollbackFor = {Exception.class}
    )
    public boolean saveBatchTypeSplit(Map<String, List<BankPublicTradePrimitive>> batMap) {
        for (Map.Entry<String, List<BankPublicTradePrimitive>> entry : batMap.entrySet()) {
            Map<String, String> tableSuffix = DynamicTableNameHelper.getTableSuffix();
            tableSuffix.put("tbl_bank_public_trade_primitive", entry.getKey());
            DynamicTableNameHelper.setTableSuffix(tableSuffix);
            saveBatch(entry.getValue());
        }
        return true;
    }
}
